package com.hqhsc.module.manage.activityuser.dao;

import com.hqhsc.core.dao.BaseJdbcDaoImpl;
import com.hqhsc.module.common.dao.CommonDao;
import com.hqhsc.module.common.domain.OffLineChannelElement;
import com.hqhsc.module.login.domain.User;
import com.hqhsc.module.login.domain.UserRowMapper;
import com.hqhsc.module.manage.activityuser.domain.ActivityUser;
import com.hqhsc.module.manage.activityuser.domain.ActivityUserRowMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@Repository
public class ActivityUserManageDao extends BaseJdbcDaoImpl {
	final Logger logger = LoggerFactory.getLogger(ActivityUserManageDao.class);

    @Autowired
    private CommonDao commonDao;

    public long getActivityUserCount(int is_import , String is_buy , long activity_id , long channel_id , String user_name) {
        String sql = "select count(1) from activity_users t where is_import = ? ";
        List<Object> list = new ArrayList<Object>();
        list.add(is_import);
        if(StringUtils.isNotEmpty(is_buy)) {
            sql = sql + " and is_buy = ? ";
            list.add(is_buy);
        }
        if(activity_id != -1) {
            sql = sql + " and activity_id = ? ";
            list.add(activity_id);
        }
        if(channel_id != -1) {
            sql = sql + " and channel_id = ? ";
            list.add(channel_id);
        }
        if(StringUtils.isNotEmpty(user_name)) {
            sql = sql + " and name like ? ";
            list.add("%"+user_name+"%");
        }
        Object[] o = list.toArray();
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().queryForLong(sql, o);
    }


    public List<ActivityUser> getActivityUserList(int is_import , String is_buy , long activity_id , long channel_id , String user_name , String orderby , String orderby_type , int start ,  int size) {

        String sql = "select * from activity_users t where is_import = ? ";
        List<Object> list = new ArrayList<Object>();
        list.add(is_import);
        if(StringUtils.isNotEmpty(is_buy)) {
            sql = sql + " and is_buy = ? ";
            list.add(is_buy);
        }
        if(activity_id != -1) {
            sql = sql + " and activity_id = ? ";
            list.add(activity_id);
        }
        if(channel_id != -1) {
            sql = sql + " and channel_id = ? ";
            list.add(channel_id);
        }
        if(StringUtils.isNotEmpty(user_name)) {
            sql = sql + " and name like ? ";
            list.add("%"+user_name+"%");
        }
        if(StringUtils.isNotEmpty(orderby)) {
            sql = sql + " order by " + orderby + " " + orderby_type;
        } else {
            sql = sql + " order by id desc ";
        }
        list.add(start);
        list.add(size);
        Object[] o = list.toArray();
        sql = sql + " limit ? ,? ";
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().query(sql, o, new ActivityUserRowMapper());
    }

    public long getActivityUserCountByChannelId(long channel_id) {
        String sql = "select count(1) from activity_users t where channel_id = ? ";
        Object[] o = new Object[] { channel_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().queryForLong(sql, o);
    }

    public List<ActivityUser> getActivityUsersByChannelId(long channel_id , int start , int size) {
        String sql = "select * from activity_users t where channel_id = ? limit ?,? ";
        Object[] o = new Object[]{channel_id , start , size};
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().query(sql, o, new ActivityUserRowMapper());
    }


    public void batchImportActivityUser(final List<OffLineChannelElement> oe_list , final long user_id) {
        String sql = "insert into activity_users (name , sex , dn , channel_id , channel_name , is_buy , nobuy_reason , buy_googds , pay_amount , is_discount , create_by) " +
                "values (?,?,?,?,?,?,?,?,?,?,?)";
        try {
            this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
//                    ps.setLong(1, oe_list.get(i).getId());
                    ps.setString(1, oe_list.get(i).getName());
                    ps.setString(2, oe_list.get(i).getSex());
                    ps.setString(3, oe_list.get(i).getDn());
                    ps.setLong(4, oe_list.get(i).getChannel_id());
                    ps.setString(5, oe_list.get(i).getChannel_name());
                    ps.setString(6, oe_list.get(i).getIs_buy());
                    ps.setString(7, oe_list.get(i).getNobuy_reason());
                    ps.setString(8, oe_list.get(i).getBuy_googds());
                    ps.setDouble(9, Double.parseDouble(oe_list.get(i).getPay_amount()));
                    ps.setString(10, oe_list.get(i).getIs_discount());
                    ps.setLong(11, user_id);
                }
                public int getBatchSize() {
                    return oe_list.size();//这个方法设定更新记录数，里面存放的都是我们要更新的，所以返回labels.length
                }
            });
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

}
